package com.hlf.modules.h5.controller;

import com.hlf.common.annotation.SysLog;
import com.hlf.common.constants.Constants;
import com.hlf.common.utils.ReturnResult;
import com.hlf.common.validator.Assert;
import com.hlf.modules.h5.annotation.LoginUser;
import com.hlf.modules.merchant.goods.mapper.GoodsMapper;
import com.hlf.modules.merchant.order.entity.Order;
import com.hlf.modules.merchant.order.service.OrderService;
import com.hlf.modules.merchant.table.entity.Table;
import com.hlf.modules.merchant.table.service.TableService;
import com.hlf.modules.wechat.model.Customer;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2017/11/5.
 */

@Api("H5订单接口")
@RestController
@RequestMapping("/h5/order/")
public class H5OrderController {

    @Autowired
    private OrderService orderService;
    @Autowired
    private TableService tableService;
    @Autowired
    private GoodsMapper goodsMapper;

    /**
     * 创建订单
     */
    @SysLog("创建订单")
    @PostMapping("create")
    @ApiOperation(value = "创建订单", notes = "创建订单")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", dataType = "string", name = "token", value = "token", required = true)
    })
    public ReturnResult create(HttpServletRequest request, @LoginUser Customer customer) {
        String tableId = String.valueOf(customer.getSpendTableId());
        return orderService.h5CreateOrder(request, tableId,customer.getCustomerId());
    }


    /**
     * h5订单详情
     */
    @GetMapping("orderList")
    @ApiOperation(value = "h5订单详情", notes = "h5订单详情")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", dataType = "string", name = "token", value = "token", required = true)
    })
    public ReturnResult orderList(@LoginUser Customer customer) {
        String tableId = String.valueOf(customer.getSpendTableId());
        return orderService.orderList(tableId);
    }

    /**
     * h5备注
     */
    @SysLog("h5订单备注")
    @PostMapping("remarks")
    @ApiOperation(value = "h5订单备注", notes = "h5订单备注")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", dataType = "string", name = "orderId", value = "订单id", required = true),
            @ApiImplicitParam(paramType = "query", dataType = "string", name = "remarks", value = "备注", required = true),
            @ApiImplicitParam(paramType = "query", dataType = "string", name = "token", value = "token", required = true)
    })
    public ReturnResult remarks(HttpServletRequest request, String orderId, String remarks) {
        Assert.isBlank(orderId, "订单id不能为空");
        Assert.isBlank(remarks, "备注不能为空");
        //添加备注
        orderService.insertReamrks(request, orderId, remarks);
        return ReturnResult.ok();
    }

    /**
     * h5订单列表
     */
    @GetMapping("list")
    @ApiOperation(value = "h5订单列表", notes = "h5订单列表")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", dataType = "string", name = "token", value = "token", required = true)
    })
    public ReturnResult list(@LoginUser Customer customer) {
        Long customerId = customer.getCustomerId();
        List<Order> orderList = orderService.h5QueryListByUserId(customerId);
        return ReturnResult.ok().put(Constants.DEFAULT_RESULT_NAME,orderList);
    }

    /**
     * h5消费者信息
     */
    @GetMapping("info")
    @ApiOperation(value = "h5消费者信息", notes = "h5消费者信息")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", dataType = "string", name = "token", value = "token", required = true)
    })
    public ReturnResult info(@LoginUser Customer customer) {
        return ReturnResult.ok().put(Constants.DEFAULT_RESULT_NAME,customer);
    }

    /**
     * 取消订单
     */
    @PostMapping("cancel")
    @ApiOperation(value = "取消订单", notes = "取消订单")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", dataType = "string", name = "orderId", value = "订单id", required = true),
            @ApiImplicitParam(paramType = "query", dataType = "string", name = "token", value = "token", required = true)
    })
    public ReturnResult cancel(@LoginUser Customer cust, String orderId) {
        Assert.isBlank(orderId, "订单id不能为空");
        //取消订单
        Long tableId = orderService.cancelOrder(cust, orderId);
        //查询餐桌
        Table table = tableService.queryObjectForId(tableId);
        Map<String, Object> map = new HashMap<>();
        map.put("date", new Date());
        map.put("tableNum", table.getTableNum());
        return ReturnResult.ok().put(Constants.DEFAULT_RESULT_NAME, map);
    }

    /**
     * 支付总价
     */
    @PostMapping("orderAmt")
    @ApiOperation(value = "支付总价", notes = "支付总价")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", dataType = "string", name = "orderId", value = "订单id", required = true),
            @ApiImplicitParam(paramType = "query", dataType = "string", name = "token", value = "token", required = true)
    })
    public ReturnResult orderAmt(@LoginUser Customer cust, String orderId) {
        Assert.isBlank(orderId, "订单id不能为空");

        System.out.println("cust 用户 = " + cust);
        Order order = orderService.queryObject(orderId);
        if (order == null){
             return ReturnResult.error("订单为找到!");
        }
        HashMap<Object, Object> map = new HashMap<>(1);
        map.put("orderAmt",order.getPayAmt());
        return ReturnResult.ok().put(Constants.DEFAULT_RESULT_NAME,map);
    }

    /**
     * 支付
     */
    @PostMapping("pay")
    @ApiOperation(value = "pay", notes = "pay")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", dataType = "string", name = "token", value = "token", required = true),
            @ApiImplicitParam(paramType = "query", dataType = "string", name = "orderId", value = "订单id", required = true),
    })
    public ReturnResult cancel1(@LoginUser Customer customer,String orderId) throws Exception {
        Assert.isBlank(orderId, "订单id不能为空");

        Map<String, Object> map = orderService.executePayRequest(customer,orderId);
        return ReturnResult.ok().put(Constants.DEFAULT_RESULT_NAME,map);
    }

}
